package cn.cloudplug.friendsrecommend.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;
import android.widget.AdapterView.OnItemClickListener;
import cn.cloudplug.friendsrecommend.R;
import cn.cloudplug.friendsrecommend.calendar.CalendarAdapter;

public class CalendarUtil {
	private ViewFlipper flipper = null;
	private CalendarAdapter calV = null;
	private RelativeLayout rl=null;
	private GridView gridView = null;
	private int jumpMonth = 0; // 每次滑动，增加或减去一个月,默认为0（即显示当前月）
	private int jumpYear = 0; // 滑动跨越一年，则增加或者减去一年,默认为0(即当前年)
	private int year_c = 0;
	private int month_c = 0;
	private int day_c = 0;
	private String currentDate = "";
	/** 每次添加gridview到viewflipper中时给的标记 */
	private int gvFlag = 0;
	/** 当前的年月，现在日历顶端 */
	private TextView currentMonth;
	private Activity activity;
	public static int screenWidth;
	int H;int W;
	private boolean isShow;
	public CalendarUtil(int screenWidth, Activity activity, ViewFlipper flipper, TextView currentMonth, RelativeLayout rl){
		this.activity = activity;
		this.rl=rl;
		CalendarUtil.screenWidth=screenWidth;
		this.flipper=flipper;
		this.currentMonth=currentMonth;
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
		currentDate = sdf.format(date); // 当期日期
		year_c = Integer.parseInt(currentDate.split("-")[0]);
		month_c = Integer.parseInt(currentDate.split("-")[1]);
		day_c = Integer.parseInt(currentDate.split("-")[2]);
	}
	public void loadCurrentCalendar() {
		flipper.removeAllViews();
		addGridView();
		calV = new CalendarAdapter(activity, activity.getResources(), jumpMonth, jumpYear, year_c, month_c, day_c);
		gridView.setAdapter(calV);
		flipper.addView(gridView, 0);
		addTextToTopTextView(currentMonth);
	}
	/**
	 * 移动到下一个月
	 * 
	 * @param gvFlag
	 */
	public void enterNextMonth(int gvFlag) {
		addGridView(); // 添加一个gridView
		jumpMonth++; // 下一个月

		calV = new CalendarAdapter(activity, activity.getResources(), jumpMonth, jumpYear, year_c, month_c, day_c);
		gridView.setAdapter(calV);
		addTextToTopTextView(currentMonth); // 移动到下一月后，将当月显示在头标题中
		gvFlag++;
		flipper.addView(gridView, gvFlag);
		flipper.setInAnimation(AnimationUtils.loadAnimation(activity, R.anim.push_left_in));
		flipper.setOutAnimation(AnimationUtils.loadAnimation(activity, R.anim.push_left_out));
		flipper.showNext();
		flipper.removeViewAt(0);
	}

	/**
	 * 移动到上一个月
	 * 
	 * @param gvFlag
	 */
	public void enterPrevMonth(int gvFlag) {
		addGridView(); // 添加一个gridView
		jumpMonth--; // 上一个月

		calV = new CalendarAdapter(activity, activity.getResources(), jumpMonth, jumpYear, year_c, month_c, day_c);
		gridView.setAdapter(calV);
		gvFlag++;
		addTextToTopTextView(currentMonth); // 移动到上一月后，将当月显示在头标题中
		flipper.addView(gridView, gvFlag);

		flipper.setInAnimation(AnimationUtils.loadAnimation(activity, R.anim.push_right_in));
		flipper.setOutAnimation(AnimationUtils.loadAnimation(activity, R.anim.push_right_out));
		flipper.showPrevious();
		flipper.removeViewAt(0);
	}

	/**
	 * 添加头部的年份 闰哪月等信息
	 * 
	 * @param view
	 */
	public void addTextToTopTextView(TextView view) {
		StringBuffer textDate = new StringBuffer();
		textDate.append(calV.getShowYear()).append("年").append(calV.getShowMonth()).append("月").append("\t");
		view.setText(textDate);
	}

	private void addGridView() {
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		// 取得屏幕的宽度和高度
		gridView = new GridView(activity);
		gridView.setNumColumns(7);
		
		//gridView.setColumnWidth(40);
		// gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
		gridView.setGravity(Gravity.CENTER_VERTICAL);
		gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		// 去除gridView边框
		gridView.setVerticalSpacing(1);
		gridView.setHorizontalSpacing(1);
		gridView.setOnTouchListener(new OnTouchListener() {
			// 将gridview中的触摸事件回传给gestureDetector

			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				return false;
			}
		});

		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> viewGroup, View view, int position, long arg3) {
				// TODO Auto-generated method stub
				// 点击任何一个item，得到这个item的日期(排除点击的是周日到周六(点击不响应))
				W=view.getWidth()+1;
				H=view.getHeight()+1;
				int startPosition = calV.getStartPositon();
				int endPosition = calV.getEndPosition();
				if (startPosition <= position + 7 && position <= endPosition - 7) {
					String scheduleDay = calV.getDateByClickItem(position).split("\\.")[0]; // 这一天的阳历
					String scheduleYear = calV.getShowYear();
					String scheduleMonth = calV.getShowMonth();
					
					if(isShow){
						hide();
						show(view,position);
					}else{
						show(view,position);
					}
					
				}
			}
		});
		gridView.setLayoutParams(params);
	}
	private View view;
	public TextView tv;
	protected void show(View view,int position) {
		this.view=view;
		tv=new TextView(activity);
		RelativeLayout.LayoutParams param=new RelativeLayout.LayoutParams(W, H/2);
		param.addRule(RelativeLayout.ALIGN_LEFT,R.id.relativelayout);
		param.addRule(RelativeLayout.ALIGN_TOP,R.id.flipper);
		float top=0;float left=0;
		top=(position/7)*H-H/4;
		left=(position%7)*W+W/2;
		param.setMargins((int)left, (int)top, 0, 0);
		tv.setBackgroundResource(R.drawable.textview_calendar_msg_frame);
		rl.addView(tv,param);
		view.setBackgroundResource(R.drawable.textview_calendar_frame);;
		isShow=true;
	}
	public void hide() {
		rl.removeView(tv);
		view.setBackgroundColor(activity.getResources().getColor(android.R.color.white));
		isShow=false;
	}
	public int getYear_c() {
		return year_c;
	}
	public void setYear_c(int year_c) {
		this.year_c = year_c;
	}
	public int getMonth_c() {
		return month_c;
	}
	public void setMonth_c(int month_c) {
		this.month_c = month_c;
	}
	public int getDay_c() {
		return day_c;
	}
	public void setDay_c(int day_c) {
		this.day_c = day_c;
	}
	public int getJumpMonth() {
		return jumpMonth;
	}
	public void setJumpMonth(int jumpMonth) {
		this.jumpMonth = jumpMonth;
	}
	public int getJumpYear() {
		return jumpYear;
	}
	public void setJumpYear(int jumpYear) {
		this.jumpYear = jumpYear;
	}
	
	
}
